package com.gy.zpchat.ui.search;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import im.zego.zim.enums.ZIMConversationType;
import com.gy.zpchat.R;

public class SearchFilterActivity extends AppCompatActivity {

    private EditText searchEditText;
    private CheckBox textMessageCheckBox;
    private CheckBox imageMessageCheckBox;
    private CheckBox audioMessageCheckBox;
    private CheckBox videoMessageCheckBox;
    private CheckBox fileMessageCheckBox;
    private EditText userIdEditText;
    private RadioButton todayRadioButton;
    private RadioButton last7DaysRadioButton;
    private RadioButton last30DaysRadioButton;
    private RadioGroup timeRadioGroup;
    private Button searchButton;
    public String conversationID;
    public String conversationName;
    public ZIMConversationType conversationType;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_filter);

        searchEditText = findViewById(R.id.searchEditText);
        textMessageCheckBox = findViewById(R.id.textMessageCheckBox);
        imageMessageCheckBox = findViewById(R.id.imageMessageCheckBox);
        audioMessageCheckBox = findViewById(R.id.audioMessageCheckBox);
        videoMessageCheckBox = findViewById(R.id.videoMessageCheckBox);
        fileMessageCheckBox = findViewById(R.id.fileMessageCheckBox);
        userIdEditText = findViewById(R.id.userIdEditText);
        todayRadioButton = findViewById(R.id.todayRadioButton);
        last7DaysRadioButton = findViewById(R.id.last7DaysRadioButton);
        last30DaysRadioButton = findViewById(R.id.last30DaysRadioButton);
        timeRadioGroup = findViewById(R.id.timeRadioGroup);
        searchButton = findViewById(R.id.searchButton);

        conversationID = getIntent().getStringExtra("conversationID");
        conversationName = getIntent().getStringExtra("conversationName");
        conversationType = ZIMConversationType.getZIMConversationType(
                getIntent().getIntExtra("conversationType", -1));

        // 获取查询条件并执行查询操作
        searchButton.setOnClickListener(v -> {
            String searchKeyword = searchEditText.getText().toString();
            boolean isTextChecked = textMessageCheckBox.isChecked();
            boolean isImageChecked = imageMessageCheckBox.isChecked();
            boolean isAudioChecked = audioMessageCheckBox.isChecked();
            boolean isVideoChecked = videoMessageCheckBox.isChecked();
            boolean isFileChecked = fileMessageCheckBox.isChecked();
            String userId = userIdEditText.getText().toString();
            int timeSelection = timeRadioGroup.getCheckedRadioButtonId();  // 返回被选中的时间单选按钮的 ID
            long startTimestamp = 0;
            long endTimestamp = 0;

            if (timeSelection == 0) {
                // 今天内
                // 获取当前时间戳（单位：秒）
                startTimestamp = System.currentTimeMillis();
                // 计算24小时前的时间戳（单位：毫秒）
                endTimestamp = startTimestamp - (24 * 60 * 60 * 1000);
            } else if (timeSelection == 1) {
                startTimestamp = System.currentTimeMillis();
                // 计算7天前的时间戳
                endTimestamp = startTimestamp - (7 * 24 * 60 * 60 * 1000);
            } else if (timeSelection == 2) {
                startTimestamp = System.currentTimeMillis();
                // 计算7天前的时间戳
                endTimestamp = startTimestamp - (30L * 24 * 60 * 60 * 1000);
            }

            SearchResultsActivity.actionStart(this,conversationID,conversationName,conversationType.value(), searchKeyword, isTextChecked, isImageChecked, isAudioChecked, isVideoChecked, isFileChecked, userId, startTimestamp, endTimestamp);

        });

    }

    public static void actionStart(Activity activity, String conversationID, String conversationName, int conversationType) {
        Intent intent = new Intent(activity, SearchFilterActivity.class);
        intent.putExtra("conversationID", conversationID);
        intent.putExtra("conversationName", conversationName);
        intent.putExtra("conversationType", conversationType);
        activity.startActivity(intent);

    }

}
